package com.lwb.ztx.user.widget;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.lwb.ztx.R;
import com.lwb.ztx.bean.NewsBean;
import com.lwb.ztx.database.greendao.db.Collect;
import com.lwb.ztx.utils.DateUtils;
import com.lwb.ztx.utils.ImageLoaderUtils;
import com.lwb.ztx.utils.ListUtils;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by luweibin on 2016/6/1.
 */
public class CollectAdapter extends RecyclerView.Adapter {
    private List<Collect> collects;
    private Context context;
    private OnItemClickListener listener;
    private OnItemSelectListener selectListener;
    private boolean isEdit = false;

    public CollectAdapter(Context context, OnItemClickListener listener,OnItemSelectListener selectListener) {
        this.context = context;
        this.listener = listener;
        this.selectListener = selectListener;
    }

    public void setCollects(List<Collect> collects) {
        this.collects = collects;
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public boolean isEdit() {
        return isEdit;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.collect_news_item, parent, false);
        return new CollectViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        Collect collect = collects.get((getItemCount() - 1) - position);
        CollectViewHolder viewHolder = (CollectViewHolder) holder;
        viewHolder.mNewsTitle.setText(collect.getCollectTitle());
        ImageLoaderUtils.display(context,viewHolder.mNewsImage,collect.getCollectImageUrl());
        viewHolder.mNewsTime.setText("发布时间: " + collect.getCollectUpdateTime());
        viewHolder.mNewsCollectTime.setText("收藏时间: " + DateUtils.formateDate(collect.getCollectTime()));
        viewHolder.index = (getItemCount() - 1) - position;
        if (listener != null) {
            NewsBean bean= new NewsBean();
            bean.setType(collect.getCollectType());
            bean.setLinkUrl(collect.getCollectLinkUrl());
            bean.setDocumentId(collect.getCollectDocId());
            bean.setUpdateTime(collect.getCollectUpdateTime());
            bean.setSource(collect.getCollectSource());
            viewHolder.bean = bean;
        }
        if (isEdit) {
            viewHolder.mNewsCheckBox.setVisibility(View.VISIBLE);
        } else {
            viewHolder.mNewsCheckBox.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return ListUtils.getSize(collects);
    }

    class CollectViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        @InjectView(R.id.collect_news_item_image)
        public ImageView mNewsImage;
        @InjectView(R.id.collect_news_item_title)
        public TextView mNewsTitle;
        @InjectView(R.id.collect_news_item_time)
        public TextView mNewsTime;
        @InjectView(R.id.collect_news_item_collect_time)
        public TextView mNewsCollectTime;
        public NewsBean bean;
        @InjectView(R.id.collect_news_item_select)
        public CheckBox mNewsCheckBox;

        public int index;

        public CollectViewHolder(View itemView) {
            super(itemView);
            ButterKnife.inject(this,itemView);
            itemView.setOnClickListener(this);
            mNewsCheckBox.setOnClickListener(this);
            mNewsCheckBox.setClickable(false);
        }

        @Override
        public void onClick(View v) {
            if (!isEdit && listener != null) {
                listener.onItemClick(bean);
            }
            if (isEdit) {
                mNewsCheckBox.setChecked(!mNewsCheckBox.isChecked());
                if (mNewsCheckBox.isChecked()){
                    selectListener.OnItemChecked(collects.get(index));
                } else {
                    selectListener.OnItemUnChecked(collects.get(index));
                }
            }
        }
    }

    public interface OnItemClickListener {
        void onItemClick(NewsBean bean);
    }

    public interface OnItemSelectListener {
        void OnItemChecked(Collect collect);
        void OnItemUnChecked(Collect collect);
    }

}
